package com.dhj;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * @author DaHuaJia
 * @Description 获取百度首页内容，demo
 * @Date 2021-11-12 18:55:09
 */
public class CrawlerFirst {

    public static void main(String[] args) throws IOException {
        // 打开浏览器，创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();

        // 输入网址，发起get请求创建HttpGet对象
        HttpGet httpGet = new HttpGet("https://www.baidu.com");

        // 按回车，发起请求，返回响应，使用HttpClient对象发起请求
        CloseableHttpResponse response = httpClient.execute(httpGet);

        // 解析响应，获取数据
        // 判断状态码是否为200
        if(response.getStatusLine().getStatusCode() == 200){
            HttpEntity entity = response.getEntity();
            String content = EntityUtils.toString(entity, "utf8");
            System.out.println(content);
        }

    }

}
